import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'
const houseTypeRange = [{
  type: 'workshop',
  text: '厂房'
}, {
  type: 'warehouse',
  text: '仓库'
}, {
  type: 'land',
  text: '土地'
}]

// 功能选择
const tabList = [{
  path: 'houseHome',
  type: 'workshop',
  photo: 'factory',
  name: '厂房',
},{
  path: 'houseHome',
  type: 'warehouse',
  photo: 'warehouse',
  name: '仓库',
},{
  path: 'houseHome',
  type: 'land',
  photo: 'ground',
  name: '土地',
},{
  path: 'businessHome',
  photo: 'made',
  name: '同城制造',
},{
  path: 'enterpriseHandling',
  photo: 'business',
  name: '企业服务',
}]

const areaTypeRange = [{
  minArea: 0,
  maxArea: 0,
  text: '不限'
}, {
  minArea: 0,
  maxArea: 500,
  text: '0~500m²'
}, {
  minArea: 500,
  maxArea: 1000,
  text: '500~1000m²'
}, {
  minArea: 1000,
  maxArea: 2000,
  text: '1000~2000m²'
}, {
  minArea: 2000,
  maxArea: 4000,
  text: '2000~4000m²'
}, {
  minArea: 4000,
  maxArea: 0,
  text: '4000m²以上'
}]

const floorTypeRange = [{
  text: '不限',
  value: 0
}, {
  text: '1楼',
  value: 1
}, {
  text: '2楼及以上',
  value: 2
}]

const landTypeRange = [{
  text: '不限',
  value: ''
}, {
  text: '硬地',
  value: 0
}, {
  text: '毛地',
  value: 1
}]

const priceRangeType = [{
  minPrice: 0,
  maxPrice: 0,
  text: '不限'
}, {
  minPrice: 0,
  maxPrice: 500,
  text: '0~500元/月'
}, {
  minPrice: 500,
  maxPrice: 1000,
  text: '500~1000元/月'
}, {
  minPrice: 1000,
  maxPrice: 2000,
  text: '1000~2000元/月'
}, {
  minPrice: 2000,
  maxPrice: 4000,
  text: '2000~4000元/月'
}, {
  minPrice: 4000,
  maxPrice: 0,
  text: '4000元/月以上'
}]
const rentTypeRange = [{
  text: '不限',
  value: -1
}, {
  text: '整租',
  value: 0
}, {
  text: '分租',
  value: 1
}]

export default {
  namespace: 'homepage',
  state: {
    listData: [], // 列表数据
    houseTypeRange,
    houseType: {
      type: 'workshop',
      text: '厂房'
    },
    tabList, // 功能选择
    helpList: [{ // 帮我选址
      name: '帮我选址',
      code: 'chose',
    },{
      name: '帮我租售',
      code: 'sell',
    }],
    helpCode: 'chose', // 帮我操作的当前选中项
    areaTypeRange,
    areaType: {
      minArea: 0,
      maxArea: 0,
      text: '不限'
    },
    floorTypeRange,
    floorType: {
      text: '不限',
      value: 0
    },
    landTypeRange,
    landType: {
      text: '不限',
      value: 0
    },
    priceRangeType,
    rentTypeRange,
    rentType: {
      text: '不限',
      value: -1
    },
    priceType: {
      minPrice: 0,
      maxPrice: 0,
      text: '不限'
    },
    districtType: {
      id: 0,
      name: '区域'
    },
    districtTypeRange: [],
    limit: 8,
    offset: 0,
    total: 0, // 总数
    moreData: {
      path: 'houseHome',
      type: 'workshop',
      name: '厂房',
    }, // 点击更多跳转页面传参
  },
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    },
    initData(state, action) {
      return {
        ...state,
        ...action.payload,
        listData: [],
        offset: 0,
        total: 0, // 总数
      }
    }
  },
  effects: {
    // 获取列表数据
    * loadList({payload = {}}, {select, put, call}) {
      const {
        limit, listData,
        houseType, areaType, priceType, districtType, rentType, floorType, landType
      } = yield select(({homepage}) => homepage)
      const {maxArea, minArea} = areaType;
      const {maxPrice, minPrice} = priceType;
      let districtId = districtType.id;
      const param = {
        limit, maxArea, minArea, maxPrice, minPrice, districtId,
        offset: listData.length,
        rentType: rentType.value,
        floor: floorType.value,
        wareType: landType.value,
      }

      let url = '/service/' + houseType.type + '/getPage';

      const {data} = yield call(request, {
        url: SERVERURL + url,
        data: param,
        method: 'POST',
      })
      if (data && data.rows instanceof Array) {
        if (data.rows.length === 0) {
          Taro.showToast({
            title: (data || {}).msg || '没有更多数据了',
            icon: 'none'
          })
        } else {
          const d = data.rows || {};
          yield put({
            type: 'updateState',
            payload: {
              listData: listData.concat(d),
              total: data.total
            }
          })
        }
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    * loadDistrict({payload = {}}, {put, call}) {
      const {data} = yield call(request, {
        url: SERVERURL + '/service/district/all',
        method: 'get',
      })
      if (data && data.data instanceof Array) {
        const d = data.data || {};
        yield put({
          type: 'updateState',
          payload: {
            districtTypeRange: d
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },

  }
};
